package com.caisejingyu.whales.home.common;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ScrollerViewpager extends ViewPager {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100002)
                setCurrentItem(getCurrentItem() + 1);
        }
    };

    //滚动的页面
    private List<View> views;

    //滚动的页面数量
    private int count;

    //自动滚动信号
    private final int SCROLL = 100002;

    //创建一个滚动线程
    private Thread thread;

    //间隔时间
    private int SPACE;

    //目前线程状态
    private boolean NORMAL = true;

    public ScrollerViewpager(@NonNull Context context) {
        super(context);
    }

    public ScrollerViewpager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 功能：初始化
     * @param views 视图组
     * @param space 间隔时间
     */
    public void init(List<View> views, final int space){
        this.views = views;
        this.count = views.size();
        this.SPACE = space;

        setAdapter(new MyAdapter());
//
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        NORMAL = true;
                        Thread.sleep(SPACE * 1000);
                        handler.sendEmptyMessage(SCROLL);
                    } catch (InterruptedException e) {
                    }
                }
            }
        });
        thread.start();
    }

    public class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int temp = position % count;
            View view = views.get(temp);
            if (view.getParent() == container) {
                container.removeView(view);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }
}